package com.util;

import java.util.*;
import java.io.*;

public class TextTools {

	public static void main(String[] args) {
		@SuppressWarnings("unused")
		TextTools textTools1 = new TextTools();
	}

	
	/**
	 * 将空格，"'"号，换行替换成html显示格式。
	 */
	public static String txt2html(String txt) {
		String html = txt;
		html = Replace(html, "&", "&amp;");
		html = Replace(html, "<", "&lt;");
		html = Replace(html, ">", "&gt;");
		html = Replace(html, "\n", "<br>");
		html = Replace(html, "\t", "    ");
		html = Replace(html, "  ", " &nbsp;");
		html = Replace(html, "'", "&#39");
		return html;

	}

	/**
	 * replace 方法
	 */

	public static String Replace(String source, String oldString,
			String newString) {
		StringBuffer output = new StringBuffer();
		int lengthOfSource = source.length();
		int lengthOfOld = oldString.length();
		int posStart;
		int pos;
		for (posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos
				+ lengthOfOld) {
			output.append(source.substring(posStart, pos));
			output.append(newString);
		}

		if (posStart < lengthOfSource) {
			output.append(source.substring(posStart));
		}
		return output.toString();
	}

	/**
	 * 
	 * 字符从html格式转换成text格式。将&lt;br&gt;替换为换行(\n)等。
	 */
	public static String html2txt(String str) {
		String strJava = str;
		strJava = Replace(strJava, "&amp;", "&");
		strJava = Replace(strJava, "&lt;", "<");
		strJava = Replace(strJava, "&gt;", ">");
		strJava = Replace(strJava, "&gb;", ">");
		strJava = Replace(strJava, "\t", "\t");
		strJava = Replace(strJava, "&nbsp;", "  ");
		// strJava=Replace(strJava,"''","'");
		strJava = Replace(strJava, "&#34", "\"");
		strJava=Replace(strJava,"<br>","\n");
		return strJava;
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：splitUserLogin 入口参数： str:传进来的用户login全名，如："aaa@agebiz.com"
	 * num:返回的数组里的位数。例： num=0时，则返回@前面的aaa；num=1时，则返回@后面的agebiz.com。
	 * 
	 * 返回类型：String 功能说明: 从用户login里分离出@前的login信息、和@后的domain信息。
	 * 
	 * ------------------------------------------------------------
	 */

	public static String splitUserLogin(int num, String str) {
		StringTokenizer st = new StringTokenizer(str, "@");
		String[] sa = new String[st.countTokens()];
		int i = 0;
		while (st.hasMoreTokens()) {
			sa[i] = st.nextToken();
			i++;
		}
		/*
		 * for(int n=0;n<i;n++) { System.out.println(sa[n]); }
		 * System.out.println("splitStr:"+sa[num]);
		 */
		return sa[num];
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：splitStr 入口参数： str:传进来的字符串，格式:|str|str2|str3| splitFlag:分割的特定标记，如"|"
	 * 
	 * 返回类型：String[] 功能说明: 把字符串按特定标记分割成数组
	 * 
	 * ------------------------------------------------------------
	 */

	public static String[] splitStr(String splitFlag, String str) {
		StringTokenizer st = new StringTokenizer(str, splitFlag);
		String[] strArray = new String[st.countTokens()];
		int i = 0;
		while (st.hasMoreTokens()) {
			strArray[i] = st.nextToken();
			i++;
		}
		return strArray;
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：splitFileName 入口参数： str:传进来的上传文件名，如："file.doc" num:返回的数组里的位数。例：
	 * num=0时，则返回.前面的file；num=1时，则返回.后面的doc。
	 * 
	 * 返回类型：String 功能说明: 从上传文件名里分离出文件类型
	 * 
	 * ------------------------------------------------------------
	 */

	public static String splitFileName(int num, String str) {
		StringTokenizer st = new StringTokenizer(str, ".");
		String[] sa = new String[st.countTokens()];
		int i = 0;
		while (st.hasMoreTokens()) {
			sa[i] = st.nextToken();
			i++;
		}
		return sa[num];
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：FileToString 入口参数： f 文件名 返回类型：String 功能说明: 把文件写入字符串
	 * 
	 * ------------------------------------------------------------
	 */

	public static String FileToString(File f) {
		String s = "";
		try {
			FileReader fr = new FileReader(f);
			BufferedReader br = new BufferedReader(fr);
			s = br.readLine();
			return s;
		} catch (Exception e) {
			e.printStackTrace();
			return s;
		}
	}

	public String HtmlSpecialChars(String str) {
		if (str == null || str.equals("")) {
			return str;
		}

		StringBuffer temp = new StringBuffer();
		int i = 0;
		while (i < str.length()) {
			// 将\n转换成<br>
			if (str.charAt(i) == '\n') {
				temp = temp.append("<br>");
			}

			// 将空格转换成&nbsp;
			else if (str.charAt(i) == ' ') {
				temp = temp.append("&nbsp;");
			}

			// 将<转换成&lt;
			else if (str.charAt(i) == '<') {
				temp = temp.append("&lt;");
			}

			// 将>转换成&gt;
			else if (str.charAt(i) == '>') {
				temp = temp.append("&gt;");
			}

			else {
				temp = temp.append(str.substring(i, i + 1));
			}

			i++;
		}

		String okstring = temp.toString();
		return okstring;
	}

	/**
	 * 将空格，"<br>
	 * "等符号删除掉。
	 */
	public static String Introduce2text(String str) {
		String strJava = str;
		// strJava=Replace(strJava,"&lt;","");
		// strJava=Replace(strJava,"&gt;","");
		// strJava=Replace(strJava,"<br>","");
		// strJava=Replace(strJava,"\t","");
		// strJava=Replace(strJava,"&nbsp;""");
		return strJava;

	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：viewByPageNo 入口参数：String PageNo 返回类型：String 功能说明: 显示指定页的商品文章内容
	 * 
	 * ------------------------------------------------------------
	 */
	public static String viewByPageNo(String content, String PageNo) {
		content = content.replaceAll("[NextPage]", "|");
		StringTokenizer st = new StringTokenizer(content, "|");
		String[] strArray = new String[st.countTokens()];
		int i = 0;
		while (st.hasMoreTokens()) {
			strArray[i] = st.nextToken();
			i++;
		}
		for (int j = 0; j < strArray.length; j++) {
			if ((Integer.parseInt(PageNo) - 1) == j) {
				content = strArray[j];
			}
		}
		return content;
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：PageNos 入口参数：String PageNo 返回类型：String 功能说明: 页码列表
	 * 
	 * ------------------------------------------------------------
	 */
	public static String PageNos(String content, String PageNo) {
		StringBuffer sb = new StringBuffer();
		String s = "";
		content = content.replace("[NextPage]", "|");
		StringTokenizer st = new StringTokenizer(content, "|");
		int pages = st.countTokens();
		if (Integer.parseInt(PageNo) - 1 > 0) {
			sb.append("<a href=\"?PageNo=" + (Integer.parseInt(PageNo) - 1)
					+ "\"><B>上一页</B></a>  ");
		} else {
			sb.append("<B>上一页</B>  ");
		}
		for (int i = 1; i <= pages; i++) {
			if (Integer.parseInt(PageNo) == i) {
				sb.append("<FONT color=\"red\"><B>" + i + "</B></FONT>  ");
			} else {
				sb.append("<a href=\"?PageNo=" + i + "\"><B>" + i
						+ "</B></a>  ");
			}
		}
		if (Integer.parseInt(PageNo) + 1 <= pages) {
			sb.append("<a href=\"?PageNo=" + (Integer.parseInt(PageNo) + 1)
					+ "\"><B>下一页</B></a>");
		} else {
			sb.append("<B>下一页</B>");
		}
		if (pages > 1) {
			s = sb.toString();
		}
		return s;
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：mkHashDir 入口参数：String physical_path,String md5str 返回类型：String 功能说明:
	 * 建立哈希目录
	 * 
	 * ------------------------------------------------------------
	 */
	public static String mkHashDir(String physical_path, String md5str) {
		String virtual_path = ""; // 虚拟目录
		String s = md5str.substring(0, 4);
		try {
			for (int i = 0; i < s.length(); i++) {
				virtual_path += "/" + s.charAt(i);
				File f = new File(physical_path + virtual_path);
				if (!f.exists()) {
					f.mkdir();
				}
			}
		} catch (Exception e) {
			System.out.print(e.toString());
		}
		return virtual_path;
	}

	/*
	 * ------------------------------------------------------------
	 * 
	 * 方法名称：File2MD5 入口参数：String filePath 返回类型：String 功能说明: 对文件进行32位的MD5加密
	 * 
	 * ------------------------------------------------------------
	 */
	public static String File2MD5(String filePath) {
		String strContent = "";
		try {
			File f = new File(filePath);
			BufferedReader reader = new BufferedReader(new FileReader(f));
			String line;
			while ((line = reader.readLine()) != null) {
				strContent += line;
			}

		} catch (IOException e) {
			System.out.print(e.toString());
		}
		MD5 md5 = new MD5();
		return md5.getMD5ofStr(strContent);
	}
}
